<?php

class SaleAction extends CommonAction
{
    //我的业务
    public function  getmyyewu()
    {
        $uid=I("session.userid",0);
        if(empty($uid)) $this->redirect("Index/logout");

        $model = new Model();
        $wherecondition = "p.custid=c.id and  p.employee=" . $uid . " and p.type=t.id and p.isdelete=0";
        $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t')
            ->where($wherecondition)
            ->field('p.name as proname,p.id as projectid,p.status,p.statusnote, p.keyword ,p.createtime ,c.*,t.name as tname')
            ->order('p.id desc')
            ->select();
        
        return $projectdata;


    }

    public  function  getmyProject()
    {
        $uid=I("session.userid");
        $project=D("project");
        $con["employee"]=array("in",$this->getMyEmployee());
        $con["isdelete"]=0;
         $this->modiProjectCon($con);
        $data=$project->where($con)->select();

        return $data;
    }
    public  function  getProjectInfo($pid)
    {
        $uid = I("session.userid");
        $project = D("projectinfoView");

        $con["employee"] = $uid;
        $con["id"] = $pid;
        $con["isdelete"]=0;
        $this->modiProjectCon($con);
        $data = $project->where($con)->find();

        return $data;
    }
    protected  function  modiProjectCon(&$con)
    {
        $myinfo=I("session.my_info");
        if($myinfo['role']==14 ||$myinfo['role']==9
            ||$myinfo['role']==10)
        {
            unset($con['employee']);
        }
        return $con;
    }

    /**
     * @param 1,字符形式,0,为数据形式
     * @return 以数组形式或者字符串形式返回数据
     */
    public  function  getMyEmployee($string=1)
    {
        $departids="";
        $uid = I("session.userid");
        $emp=M("employee");
        $myinfo = I("session.my_info");
        $deparemnt = $myinfo["department"];
        $emprole=$myinfo["role"];

        switch($emprole)
        {
            case "17":
                $users = $emp->where("id=$uid")->select();
                //return $users;
                break;
            case  "4":
                $_con["role"]=array("exp","in(17)");
                $_con['id'] = $myinfo['id'];
                $_con['_logic'] = 'or';
                $users = $emp->where($_con)->select();
                break;
            case "9":
                $users = $emp->select();
                break;
            default:
                $depart_model = M("department");
                $t_depart = $depart_model->find($deparemnt);
                $path = $t_depart["path"];

                $condition["path"] = array("like", "$path%");
                $depart_datas = $depart_model->where($condition)->select();

                foreach ($depart_datas as $k => $v)
                {
                    $departids .= $v["id"] . ",";
                }
                $departids = substr($departids, 0, -1);
                $t_con["department"] = array("in", $departids);
                $users = $emp->where($t_con)->select();
                break;
        }

        $userids = "";
        if ($string)
        {
            foreach ($users as $kv => $vv)
            {
                $userids .= $vv["id"] . ",";
            }
            $userids = substr($userids, 0, -1);
            return $userids;
        }
        return $users;
    }

    public  function  getCustom($pagenum,$condition=array())
    {
        $con1 = array();
        $emps = $this->getMyEmployee();
        $con1["adduser"] = array("exp", "in($emps)");
        $con = array_merge($con1, $condition);
        
        return $this->getPageData("custom", $pagenum, $con);

    }


    protected  function  getPageData($tabname,$pagenum,$condition,$order='id desc')
    {
        $model=D($tabname);
        import("ORG.Util.Page");
        $pagsize=M('employee')->where('id='.I("session.userid"))->getField("pagesize");
        $totalnum=$model->where($condition)->count();

       // dump($model->getLastSql());
        $list=$model->where($condition)->page($pagenum)->order($order)->limit($pagsize)->select();

        //dump($model->getLastSql());

        $page=new Page($totalnum,$pagsize);
        $page->rollPage=$pagsize;
        $page->setConfig("prev","上一页");
        $page->setConfig("next","下一页");
        $page->setConfig("first","首页");
        $page->setConfig("last","末页");
        $page->setConfig("theme","共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");


        unset($data);
        $data["list"]=$list;
        $data["pageshow"]=$page->show();
        return $data;
    }




}